package com.htu.edu.controller;

import com.htu.edu.bean.DeliveryAddress;
import com.htu.edu.common.R;
import com.htu.edu.service.DeliveryAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
// 2. 设置该模块的请求路径，如/user
@RequestMapping("/DeliveryAddress")
// 3. 解决跨域
@CrossOrigin
public class DeliveryAddressController {
    @Autowired
    private DeliveryAddressService deliveryAddressService;
    @PostMapping("/listDeliveryAddressByUserId")
   public R listDeliveryAddressByUserId(String userId){

        try {
            List<DeliveryAddress> list = deliveryAddressService.listDeliveryAddressByUserId(userId);
            return R.ok().data(list);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    @PostMapping("/removeDeliveryAddress")
    public R  removeDeliveryAddress(Integer daId){
        try {
            int row = deliveryAddressService.removeDeliveryAddress(daId);
            return R.ok().data(row);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    @PostMapping("/saveDeliveryAddress")
    public  R saveDeliveryAddress(DeliveryAddress address){
        try {
            int row = deliveryAddressService.saveDeliveryAddress(address);
            return R.ok().data(row);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    @PostMapping("/getDeliveryAddressById")
    public R getDeliveryAddressById(Integer daId){
        try {
            DeliveryAddress address = deliveryAddressService.getDeliveryAddressById(daId);
            return R.ok().data(address);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @PostMapping("/updateDeliveryAddress")
    public R updateDeliveryAddress(DeliveryAddress address){
        try {
            int row = deliveryAddressService.updateDeliveryAddress(address);
            return R.ok().data(row);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
